package com.mine.frame.mylandemo.mvp.presenter

import com.mine.frame.common.base.BasePresenter
import com.third.device.mg.serialport.MGSerialPort
import com.mine.frame.mylandemo.mvp.iview.IConnect
import com.mine.frame.mylandemo.ui.ConnectAtv
import com.mine.frame.mylandemo.utils.MyConvertUtils

/**
 * company 广东玛格家居有限公司
 * @author smoker
 * @Date 2021/12/17
 * Description:
 */
class ConnectPresenter(mIView: IConnect) : BasePresenter<IConnect>(mIView) {
    private val addr = 0x01.toByte()//当前设备地址


    /**
     * 解析服务器返回的指令指令
     */
    fun parseCommand(command: ByteArray) {
        when (mIView.getReadMode()) {
            ConnectAtv.READ_TEMP_AIR_ADD,
            ConnectAtv.READ_TEMP_AIR_SUBTRACT -> {
                //单个设备读取当前的温度
                if (command[0] == addr) {
                    val high = command[3]
                    val low = command[4]
                    val temp = MyConvertUtils.twoBytesToInt(low ,high)
                    mIView.showToast("当前温度:$temp℃")
                    mIView.updateNowTemp(temp)
                    if (mIView.getReadMode() == ConnectAtv.READ_TEMP_AIR_ADD) {
                        addTemp()
                    } else {
                        subtractTemp()
                    }
                }
            }
        }
        mIView.updateReadMode("")
    }


    /**
     * 空调开机/关机
     */
    fun commandAirOnOff(on: Boolean): ByteArray =
        MGSerialPort.instance.getForick().panel3In1().onOffAir(addr, on)

    /**
     * 空调模式的指令
     * [mode]0:制冷 1:制热 2:除湿 3:送风
     */
    fun commandAirWorkMode(mode: Int) =
        MGSerialPort.instance.getForick().panel3In1().changeModeAir(addr, mode)


    /**
     * 空调风速切换
     * [mode]0:自动 1:低速 2:中速 3:高速
     */
    fun commandAirWindMode(mode: Int) =
       MGSerialPort.instance.getForick().panel3In1().changeWindModeAir(addr, mode)

    /**
     * 设置制冷温度
     */
    fun setTemp(temp: Int) = MGSerialPort.instance.getForick().panel3In1().setTemperatureAir(addr, temp)

    /**
     * 获取当前面板的温度
     */
    fun getTemp(mode: String): ByteArray {
        mIView.updateReadMode(mode)
        return MGSerialPort.instance.getForick().panel3In1().getTemperatureAir(addr)
    }

    /**
     * 温度加1
     */
    fun addTemp() {
        var temp = 0
        val command = if (mIView.getNowTemp() == -1) {
            getTemp(ConnectAtv.READ_TEMP_AIR_ADD)
        } else {
            temp = if (mIView.getNowTemp() + 1 > 30) {
                30
            } else {
                mIView.getNowTemp() + 1
            }
            MGSerialPort.instance.getForick().panel3In1().setTemperatureAir(addr, temp)
        }
        mIView.getMineConnectService()?.sent(command)
        mIView.updateNowTemp(temp)
    }

    /**
     * 温度减1
     */
    fun subtractTemp() {
        var temp = 0
        val command = if (mIView.getNowTemp() == -1) {
            getTemp(ConnectAtv.READ_TEMP_AIR_SUBTRACT)
        } else {
            temp = if (mIView.getNowTemp() - 1 < 16){
                16
            }else{
                mIView.getNowTemp() - 1
            }
            MGSerialPort.instance.getForick().panel3In1().setTemperatureAir(addr, temp)
        }
        mIView.getMineConnectService()?.sent(command)
        mIView.updateNowTemp(temp)
    }

    /**
     * 空调阀控制
     */
    fun valveAirControl(type:Int,mode: Int){
        val command = MGSerialPort.instance.getForick().panel3In1().changeValveModeAir(addr ,type ,mode)
        mIView.getMineConnectService()?.sent(command)
    }
}